package logic

import (
	"context"
	"fmt"
	"gorm.io/gorm"
	"src/models/mysql"

	"src/internal/svc"
	"src/src"

	"github.com/zeromicro/go-zero/core/logx"
)

type OrderUpdateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewOrderUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *OrderUpdateLogic {
	return &OrderUpdateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *OrderUpdateLogic) OrderUpdate(in *src.OrderUpdateRequest) (*src.OrderUpdateResponse, error) {
	// todo: add your logic here and delete this line
	var update mysql.Order
	mysql.DB.Debug().Model(&update).Where("id = ?", in.Id).Find(&update)

	if update.ID == 0 {
		return nil, fmt.Errorf("订单不存在")
	}
	updates := mysql.Order{
		Model:  gorm.Model{ID: uint(in.Id)},
		Status: int(in.Status),
	}
	mysql.DB.Debug().Updates(&updates)

	return &src.OrderUpdateResponse{Id: in.Id}, nil
}
